package mysql

import (
	"home-view/homerpc/homerpc"

	"gorm.io/gorm"
)

type Comment struct {
	gorm.Model
	UserId  int64  `gorm:"type:int(11);not null;comment:'用户ID'"`
	ClassId int64  `gorm:"type:int(11);not null;comment:'课程ID'"`
	Content string `gorm:"type:varchar(300);not null;comment:'评论内容'"`
}

func (c *Comment) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Comment) CommentList(db *gorm.DB, id int64, lists []*homerpc.CommentList) ([]*homerpc.CommentList, error) {
	/*
		SELECT
		  users.id AS user_id,comments.content
		  FROM
		  comments
		  LEFT JOIN users ON users.id = comments.user_id
		  WHERE class_id=2
	*/
	err := db.Debug().Model(c).
		Select("users.id AS user_id,comments.content").
		Joins("LEFT JOIN users ON users.id = comments.user_id").
		Where("class_id=?", id).
		Order("comments.created_at desc").Find(&lists).Error
	return lists, err
}
